Page({
  data: {
    dialogVisible: false
  },
  // 监听滑块动作
  swipeClose(ev) {
    const { position, instance } = ev.detail
    // 拿到id是用来删除后端数据，index用来删除data里的数据
    const { id, index } = ev.mark

    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
        id, index
      })

      // swiper-cell 滑块关闭
      instance.close()
    }
  },

  goDetail({ mark: { id }}) {
    wx.navigateTo({
      url: `/house_pkg/pages/detail/index?id=${id}`
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index'
    })
  },
  async getRoomList() {
    // console.log('token:', getApp().token)
    const { code, data: roomList } = await wx.http.get('room')
    if (code !== 10000) return wx.utils.toast()
    console.log(roomList)
    // 更新数据
    this.setData({ roomList })
  },

  onShow() {
    this.getRoomList()
  },
  async dialogClose({ detail }) {
    // 当点击弹框的确认按钮才执行删除操作
    if (detail !== 'confirm') return
    // 从后端删除
    const { code } = await wx.http.delete(`room/` + this.data.id)
    // console.log(res)
    if (code !== 10000) return wx.utils.toast('删除数据失败，请稍后重试')
    // 更新数据
    this.data.roomList.splice(this.data.index, 1)
    this.setData({
      roomList: this.data.roomList
    })
  }
})
